/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:assign-tracer
 * Create Time: 2016年1月25日 上午11:08:12
 */
package com.lenovo.lmrp.server.assigntracer.mock;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.lenovo.lmrp.api.assigntrace.ITraceTicket;
import com.lenovo.lmrp.api.constant.QueryEntity;
import com.lenovo.lmrp.api.model.RestResponse;
import com.lenovo.lmrp.api.model.entity.TicketExtensePO;
import com.lenovo.lmrp.api.model.entity.TicketPO;
import com.lenovo.lmrp.api.model.view.AttachmentVO;
import com.lenovo.lmrp.api.model.view.SearchListFilter;
import com.lenovo.lmrp.api.model.view.TicketCost;
import com.lenovo.lmrp.api.model.view.TicketStatistics;
import com.lenovo.lmrp.api.model.view.TicketTotalVO;
import com.lenovo.lmrp.api.model.view.TicketVO;
import com.lenovo.sdc.framework.annotation.RestService;
import com.lenovo.sdc.framework.util.AttributeBean;
import com.lenovo.sdc.framework.util.CommonUtil;

/**
 * @author zhangzhenga
 *
 */
@RestService
@Path("/assigntrace/v1")
@Produces("application/json;charset-utf-8")
@Consumes("application/json;charset-utf-8")
public class TraceTicketImpl implements ITraceTicket {

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#getEntityList(java.lang.String, java.lang.String, int, int, java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
     */
    public RestResponse<List<TicketVO>> getEntityList(String itsmcode, String entity, SearchListFilter conditionVO) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
        if (!CommonUtil.isNullOrEmpty(conditionVO.getTimeRange())) {
            String[] tr = conditionVO.getTimeRange().split("-");
            try {
                Date timeStart = format.parse(tr[0]);
                if (!CommonUtil.isNullOrEmpty(tr[1])) {
                    Date timeEnd = format.parse(tr[1]);
                }
            }
            catch (ParseException e) {
                return new RestResponse<List<TicketVO>>(1, "错误的时间格式.", null);
            }
        }
        List<TicketVO> ticketVOs = new ArrayList<TicketVO>();
        for (int i = 0; i < 3; i++) {
            TicketVO vo = new TicketVO();
            vo.setTicketid(String.valueOf(i));//工单ID
            vo.setTitle("测试工单" + i);//工单标题
            vo.setProject("MS");//项目
            vo.setPriority("中");//优先级
            vo.setCaddress("西北旺东路10号院2号楼");//客户地址
            vo.setcTime(new Date());//创建时间
            vo.setPosition("40.0531760121:116.3041211900");//上门地址经纬度
            if (conditionVO.getStatus() == 1) {//接单
                Map<String, Object> sta = new HashMap<>();
                sta.put("code", "接单时效");
                sta.put("value", new Date());
                vo.setStatus(sta);
            }
            else if (conditionVO.getStatus() == 0) {//抢单

            }
            else if (conditionVO.getStatus() == 2) {//活动工单
                Map<String, Object> sta = new HashMap<>();
                sta.put("code", "预约时效");
                sta.put("value", new Date());
                vo.setStatus(sta);
            }
            else if (conditionVO.getStatus() == 4) {//历史工单
                vo.setAmount(103.2);//收益
            }
            Map<String, Object> shortcut = new HashMap<>();
            shortcut.put("name", "预约");
            shortcut.put("icon", "appoint.png");
            shortcut.put("link", "activitid");
            vo.setShortcut(shortcut);
            ticketVOs.add(vo);
        }
        return new RestResponse<List<TicketVO>>(0, "OK", ticketVOs);
    }

    @Override
    public RestResponse<Object> getEntityDetail(String itsmcode, String entity, String entityid, String includes) {
        QueryEntity q = QueryEntity.valueOf(entity.toUpperCase());
        Object o = null;
        switch (q) {
            case TICKET:
                TicketPO t = new TicketPO();
                t.setCaption("订单标题");
                t.setDescription("订单描述");
                o = t;
                break;
            case TICKET_EXTENSE:
                TicketExtensePO epo = new TicketExtensePO();
                StringBuilder sb = new StringBuilder();
                sb.append("{\"surveynote\":\"测试note\",");
                sb.append("\"customername\":\"客户名称\",");
                sb.append("\"customermobile\":\"客户电话\",");
                sb.append("\"customeraddress\":\"客户地址\"}");
                epo.setExtprops(sb.toString());
                o = epo;
                break;
            default:
                break;
        }
        return new RestResponse<Object>(0, "OK", o);
    }

    /** 
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#updateTicket(java.lang.String, java.lang.String, java.lang.String, java.util.Map)
     */
    @Override
    public RestResponse<String> updateTicket(String itsmcode, String ticketid, String action, Map fields) {
        return null;
    }

    /** 
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#uploadAttachment(java.lang.String, java.lang.String, com.lenovo.lmrp.api.model.view.AttachmentVO)
     */
    @Override
    public void uploadAttachment(String itsmcode, String ticketid, AttachmentVO attachmentVO) {
    }

    /** 
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#ticketsStatistics(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public RestResponse<TicketStatistics> ticketsStatistics(String itsmcode, String conditions, String groupby,
                                                            String orderby) {
        return null;
    }

    /** 
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#getTicketCost(java.lang.String, java.lang.String)
     */
    @Override
    public RestResponse<TicketCost> getTicketCost(String itsmcode, String ticketid) {
        return null;
    }

    /** 
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#getEngineersBill(java.lang.String, java.lang.String)
     */
    @Override
    public Object getEngineersBill(String itsmcode, String engineerid) {
        return null;
    }

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#getEntityTotal(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public <T> RestResponse<T> getEntityTotal(String itsmcode, String entity, String engnieer) {
        TicketTotalVO total = new TicketTotalVO();
        total.setAcceptqty(10);
        total.setGrabqty(4);
        return new RestResponse<T>(0, "ok", (T)total);
    }

    /* (non-Javadoc)
     * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#getGrabResult(java.lang.String, java.lang.String)
     */
    @Override
    public RestResponse<Object> getGrabResult(String itsmcode, String engineer,String ticketid) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", 1);
        result.put("link", "pageid");
        return new RestResponse<Object>(0, "抢单成功", result);
    }

	/** 
	 * @see com.lenovo.lmrp.api.assigntrace.ITraceTicket#getProjectEngineers(java.lang.String, java.lang.String)
	 */
	@Override
	public RestResponse<List<Map<String, String>>> getProjectEngineers(String itsmcode, String ticketid) {
		// TODO Auto-generated method stub
		return null;
	}


}
